package com.karl.mytask_2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.bean.MyUser;
import com.karl.mytask_2.tools.BaseActivity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Karl on 2016/5/20.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private TextView mToolBarTitle;
    private ImageButton mBack;

    private EditText mUserName,mEmail,mPass,mPass2,mPhone;
    private Button mRegisterBtn;
    String username,email,pass,pass2,phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register);
        initView();
        initEvent();
    }
    private void initView(){
        mToolBarTitle = (TextView) findViewById(R.id.tool_bar2_title);
        mBack = (ImageButton) findViewById(R.id.tool_bar2_back);

        mUserName = (EditText) findViewById(R.id.register_name);
        mEmail = (EditText) findViewById(R.id.register_email);
        mPhone = (EditText) findViewById(R.id.register_phone);
        mPass = (EditText) findViewById(R.id.register_password);
        mPass2 = (EditText) findViewById(R.id.register_password2);
        mRegisterBtn = (Button) findViewById(R.id.register_btn);
    }
    private void initEvent(){
        mBack.setOnClickListener(this);
        mToolBarTitle.setText("注册");

        mRegisterBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.register_btn:
                Toast.makeText(RegisterActivity.this, "请稍等", Toast.LENGTH_SHORT).show();
                toRegister();
                break;
            case R.id.tool_bar2_back:
                RegisterActivity.this.finish();
                break;
        }
    }
    //调用Bmob方法进行注册
    private void toRegister(){
        getEditTextContent();
        if (pass.equals(pass2)){
            if (username.equals("") || email.equals("") || pass.equals("")|| phone.equals("")){
                Toast.makeText(RegisterActivity.this, "请检查输入,所有项都是必填！", Toast.LENGTH_SHORT).show();
            }else{
                //注册部分代码
                MyUser user = new MyUser();
                user.setUsername(email);
                user.setNickName(username);
                user.setEmail(email);
                user.setPassword(pass);
                user.setMobilePhoneNumber(phone);

                user.signUp(RegisterActivity.this, new SaveListener() {
                    @Override
                    public void onSuccess() {
                        //注册成功，进入主页
                        startActivity(new Intent().setClass(RegisterActivity.this,MainActivity.class));

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        Toast.makeText(RegisterActivity.this, "注册失败，请检查网络或者换个邮箱、用户名试试！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }else {
            Toast.makeText(RegisterActivity.this, "两次密码不一致！", Toast.LENGTH_SHORT).show();
            mPass2.setText("");
        }



    }

    //获取EditText内容
    private void getEditTextContent(){
        username = mUserName.getText().toString();
        email = mEmail.getText().toString();
        pass = mPass.getText().toString();
        pass2 = mPass2.getText().toString();
        phone = mPhone.getText().toString();
    }


}
